import {removeUser} from '@/utils/cookie';

export const state = () => ({
  showModal: {
    login: false,
    register: false,
    miss: false,
    identity: false,
    logout: false,
  },
  showMenu: false,
  showMsg: false,
  authUser: {},
})

export const mutations = {
  SET_USER (state, user) {
    state.authUser = user
  },
  SET_MODAL (state, obj = { login: false, register: false, miss: false, identity: false, logout: false}) {
    state.showModal = obj
  },
  SET_MENU (state, show) {
    state.showMenu = show
  },
  SET_MSG (state, show) {
    state.showMsg = show
  },
}

export const actions = {
  // nuxtServerInit is called by Nuxt before server-rendering every page
  nuxtServerInit ({ commit }, { req }) {
    console.log('===============');
    console.log(req);
    console.log('===============');

    // if (req.session && req.session.authUser) {
    //   commit('SET_USER', req.session.authUser)
    // }
  },
  toggleModal({commit}, obj = { login: false, register: false, miss: false, identity: false, logout: false}) {
   commit('SET_MODAL', obj)
  },
  toggleMenu({commit}, show = false) {
   commit('SET_MENU', show)
  },
  toggleMsg({commit}, show = false) {
   commit('SET_MSG', show)
  },
  userInfo ({ commit }, data) {
    commit('SET_USER', data)
  },
  logout ({ commit }) {
    removeUser();
    commit('SET_MODAL', { logout: false })
    commit('SET_USER', {usertype: 0})
    if (this.$router.currentRoute.name !== 'index') {
      this.$router.replace('/')
    }
  }
}


export default {
  namespaced: true,
  state,
  mutations,
  actions
}
